package com.zy.design.ocp.old;

import com.zy.design.ocp.base.Notification;
import com.zy.design.ocp.base.NotificationEmergencyLevel;

/**
 * @author zhaoyi
 * @date 2021/3/15 17:11
 * 开闭原则 OCP
 * --- 对扩展开放、修改关闭
 */
public class Alert {

    private AlertRule rule;
    private Notification notification;

    public Alert(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    // 改动一：添加参数 timeoutCount
    public void check(String api, long requestCount, long errorCount, long durationOfSeconds,long timeoutCount) {
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY, "...");
        }
        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(NotificationEmergencyLevel.SEVERE, "...");
        }

        // 改动二：添加接口超时处理逻辑
        long timeoutTps = timeoutCount / durationOfSeconds;
        if (timeoutTps > rule.getMatchedRule(api).getMaxTimeoutTps()){
            notification.notify(NotificationEmergencyLevel.URGENCY, "...");
        }

    }

}
